var e = require;
var t = module;
var o = exports;
var n = o,
    i = function (e, t, o, n) {
        return new (o || (o = Promise))(function (i, a) {
            function r(e) {
                try {
                    c(n.next(e));
                } catch (t) {
                    a(t);
                }
            }
            function s(e) {
                try {
                    c(n.throw(e));
                } catch (t) {
                    a(t);
                }
            }
            function c(e) {
                var t;
                e.done
                    ? i(e.value)
                    : ((t = e.value),
                      t instanceof o
                          ? t
                          : new o(function (e) {
                                e(t);
                            })).then(r, s);
            }
            c((n = n.apply(e, t || [])).next());
        });
    },
    a = function (e, t) {
        var o,
            n,
            i,
            a,
            r = {
                label: 0,
                sent: function () {
                    if (1 & i[0]) throw i[1];
                    return i[1];
                },
                trys: [],
                ops: []
            };
        return (
            (a = {next: s(0), throw: s(1), return: s(2)}),
            "function" == typeof Symbol &&
                (a[Symbol.iterator] = function () {
                    return this;
                }),
            a
        );
        function s(e) {
            return function (t) {
                return c([e, t]);
            };
        }
        function c(a) {
            if (o) throw new TypeError("Generator is already executing.");
            for (; r; )
                try {
                    if (
                        ((o = 1),
                        n &&
                            (i = 2 & a[0] ? n.return : a[0] ? n.throw || ((i = n.return) && i.call(n), 0) : n.next) &&
                            !(i = i.call(n, a[1])).done)
                    )
                        return i;
                    switch (((n = 0), i && (a = [2 & a[0], i.value]), a[0])) {
                        case 0:
                        case 1:
                            i = a;
                            break;
                        case 4:
                            return r.label++, {value: a[1], done: !1};
                        case 5:
                            r.label++, (n = a[1]), (a = [0]);
                            continue;
                        case 7:
                            (a = r.ops.pop()), r.trys.pop();
                            continue;
                        default:
                            if (!(i = (i = r.trys).length > 0 && i[i.length - 1]) && (6 === a[0] || 2 === a[0])) {
                                r = 0;
                                continue;
                            }
                            if (3 === a[0] && (!i || (a[1] > i[0] && a[1] < i[3]))) {
                                r.label = a[1];
                                break;
                            }
                            if (6 === a[0] && r.label < i[1]) {
                                (r.label = i[1]), (i = a);
                                break;
                            }
                            if (i && r.label < i[2]) {
                                (r.label = i[2]), r.ops.push(a);
                                break;
                            }
                            i[2] && r.ops.pop(), r.trys.pop();
                            continue;
                    }
                    a = t.call(e, r);
                } catch (s) {
                    (a = [6, s]), (n = 0);
                } finally {
                    o = i = 0;
                }
            if (5 & a[0]) throw a[1];
            return {value: a[0] ? a[1] : void 0, done: !0};
        }
    };
Object.defineProperty(n, "__esModule", {value: !0});
var r = e("AssetsManager"),
    s = e("AssetsMap"),
    c = e("BundleManager"),
    l = e("GameSceneMediator"),
    d = e("BundleNames"),
    u = (function () {
        function e() {
            (this._bundle = null), (this._alreadyPopupList = []);
        }
        return (
            Object.defineProperty(e, "instance", {
                get: function () {
                    return e._instance || (e._instance = new e()), e._instance;
                },
                enumerable: !1,
                configurable: !0
            }),
            (e.prototype.init = function () {
                var e = this;
                this.loadBundle()
                    .then(function () {
                        return e.bundleLoadSuccess(), e.loadAssets();
                    })
                    .then(function () {
                        e.assetsLoadSuccess();
                    });
            }),
            (e.prototype.loadBundle = function () {
                var e = this;
                return new Promise(function (t, o) {
                    return i(e, void 0, void 0, function () {
                        return a(this, function () {
                            return (
                                c.default.instance.loadBundle(
                                    d.default.Popup,
                                    null,
                                    function (e) {
                                        t(e);
                                    },
                                    function (e) {
                                        o(e);
                                    }
                                ),
                                [2]
                            );
                        });
                    });
                });
            }),
            (e.prototype.loadAssets = function () {
                var e = this;
                return new Promise(function (t, o) {
                    return i(e, void 0, void 0, function () {
                        var e, n, i, r, c, l;
                        return a(this, function (a) {
                            switch (a.label) {
                                case 0:
                                    for (n in (a.trys.push([0, 5, , 6]), (e = []), s.default.PopupMap)) e.push(n);
                                    (i = 0), (a.label = 1);
                                case 1:
                                    return i < e.length
                                        ? ((r = e[i]), (c = s.default.PopupMap[r]), [4, this.loadPopupAssets(c, !0)])
                                        : [3, 4];
                                case 2:
                                    a.sent(), (a.label = 3);
                                case 3:
                                    return i++, [3, 1];
                                case 4:
                                    return t(null), [3, 6];
                                case 5:
                                    return (l = a.sent()), o(l), [3, 6];
                                case 6:
                                    return [2];
                            }
                        });
                    });
                });
            }),
            (e.prototype.loadPopupAssets = function (e, t) {
                var o = this;
                if (!e.name) throw new Error("弹窗未配置name属性，请检查后再试");
                return new Promise(function (n, s) {
                    return i(o, void 0, void 0, function () {
                        var o, i, c, l, d, u, p, h, f, _, y, m, g, v, w;
                        return a(this, function (a) {
                            switch (a.label) {
                                case 0:
                                    if (-1 !== this._alreadyPopupList.indexOf(e.name)) return n(null), [2];
                                    if (
                                        ((o = {}),
                                        (i = {assetsType: cc.Prefab, assetsList: []}).assetsList.push(e.assetsPath),
                                        (o.prefabs = i),
                                        (c = e.preAssets))
                                    )
                                        for (l in c) {
                                            for (p in ((d = c[l]),
                                            (u = o[l] || {assetsType: d.assetsType, assetsList: []}),
                                            d.assetsList))
                                                u.assetsList.push(d.assetsList[p]);
                                            o[l] = u;
                                        }
                                    a.label = 1;
                                case 1:
                                    for (f in (a.trys.push([1, 6, , 7]), (h = []), o)) h.push(f);
                                    (_ = 0), (a.label = 2);
                                case 2:
                                    return _ < h.length
                                        ? ((y = h[_]),
                                          (m = o[y]),
                                          (g = m.assetsType),
                                          (v = m.assetsList),
                                          [4, r.default.instance.loadResArray(this._bundle, v, g, t)])
                                        : [3, 5];
                                case 3:
                                    a.sent(), (a.label = 4);
                                case 4:
                                    return _++, [3, 2];
                                case 5:
                                    return (
                                        t
                                            ? this.loadPopupAssets(e, !1)
                                                  .then(function () {
                                                      n(null);
                                                  })
                                                  .catch(function (e) {
                                                      s(e);
                                                  })
                                            : (n(null), this._alreadyPopupList.push(e.name)),
                                        [3, 7]
                                    );
                                case 6:
                                    return (w = a.sent()), s(w), [3, 7];
                                case 7:
                                    return [2];
                            }
                        });
                    });
                });
            }),
            (e.prototype.bundleLoadSuccess = function () {
                (this._bundle = c.default.instance.getBundle(d.default.Popup)),
                    this._bundleLoadCallback && this._bundleLoadCallback(),
                    (this._bundleLoadCallback = null);
            }),
            (e.prototype.assetsLoadSuccess = function () {
                game.log("assetsLoadSuccess");
            }),
            (e.prototype.showPopView = function (e, t, o, n) {
                var i = this;
                return this._bundle
                    ? -1 === this._alreadyPopupList.indexOf(e.name)
                        ? (game.UIManager.instance.showLoading(99999),
                          void this.loadPopupAssets(e, !0).then(function () {
                              game.UIManager.instance.hideLoading(), i.showPopView(e, t, o || !1, n || !1);
                          }))
                        : void game.appFacade.retrieveMediator(l.default.NAME).pushPopupView(e, t, o, n)
                    : (game.UIManager.instance.showLoading(10, function () {
                          i._bundleLoadCallback = null;
                      }),
                      void (this._bundleLoadCallback = function () {
                          game.UIManager.instance.hideLoading(), i.showPopView(e, t, o, n);
                      }));
            }),
            (e.prototype.removePopupView = function (e, t) {
                game.appFacade.retrieveMediator(l.default.NAME).removePopupView(e, t || !1);
            }),
            (e._instance = null),
            e
        );
    })();
n.default = u;
